package cn.yeyiu.sexyiu.utils

import cn.yeyiu.sexyiu.bean.MangaInfo
import org.springframework.stereotype.Service
import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.util.*
import javax.servlet.http.HttpServletRequest

@Service
class NetTool {

    fun getUrl(request: HttpServletRequest, needPort: Boolean): String {
        val Urls = request.requestURL
        val Url = Urls.substring(Urls.indexOf(":") + 3)
        val host = Url.substring(0, Url.indexOf(":"))
        val port = Url.substring(Url.indexOf(":") + 1, Url.indexOf("/"))
        return if (needPort) {
            "$host:$port"
        } else host
    }

    // 去除回环接口，子接口，未运行接口
    val ip: String
        get() {
            try {
                val allNetInterfaces = NetworkInterface.getNetworkInterfaces()
                while (allNetInterfaces.hasMoreElements()) {
                    val netInterface = allNetInterfaces.nextElement() as NetworkInterface

                    // 去除回环接口，子接口，未运行接口
                    if (netInterface.isLoopback || netInterface.isVirtual || !netInterface.isUp) {
                        continue
                    }
                    if (!netInterface.displayName.contains("Intel")
                        && !netInterface.displayName.contains("Realtek")
                    ) {
                        continue
                    }
                    val addresses = netInterface.inetAddresses
                    while (addresses.hasMoreElements()) {
                        val ip = addresses.nextElement() as InetAddress
                        if (ip is Inet4Address) {
                            return ip.getHostAddress()
                        }
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return "无法获取"
        }

    fun getPort(request: HttpServletRequest): String {
        return request.localPort.toString()
    }

    companion object {
        fun encodeUrl(str: String): String {
            return if ("" == str) {
                ""
            } else {
                val str1 = String(str.toByteArray(StandardCharsets.UTF_8))
                URLEncoder.encode(str1, StandardCharsets.UTF_8)
                    .replace("+", "%20")
            }
        }

        fun buildMangaLogoPath(title: String): String {
            return "/file/manga/" + encodeUrl(title) + "/logo"
        }

        fun buildMangaLogoPath(mangaInfo: MangaInfo): String {
            return "/file/manga/" + encodeUrl(mangaInfo.title) + "/logo"
        }

        fun buildLocalMangaEpLogoPath(title: String, ep: String): String {
            return "/file/manga/" + encodeUrl(title) + "/" + encodeUrl(ep) + "/logo"
        }

        fun buildMangaImgPath(mangaEpPath: String, i: Int): String {
            return "/file/manga/" + encodeUrl(mangaEpPath)
                .replace("%2F", "/") + String.format(Locale.CHINA, "/%03d", i)
        }

        fun buildMangaImgPath(mangaTitle: String, epTitle: String, i: Int): String {
            return "/file/manga/" + encodeUrl(mangaTitle) + "/" + encodeUrl(epTitle) + String.format(
                Locale.CHINA,
                "/%03d",
                i
            )
        }
    }
}